import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import { getServiceConfig } from '../ormconfig';
// import { SerializeInterceptor } from './interceptors/serialize.interceptor';
// import { AllExceptionFilter } from './filters/all-exception.filter';
import helmet from 'helmet';
import rateLimit from 'express-rate-limit';

export const setupApp = (app: INestApplication) => {
  const config = getServiceConfig();

  const flag: boolean = config['LOG_ON'] === 'true';

  flag && app.useLogger(app.get(WINSTON_MODULE_NEST_PROVIDER));
  app.setGlobalPrefix('api/v1');

  // 全局异常过滤器
  // const httpAdapter = app.get(HttpAdapterHost);
  // const logger = new Logger();
  // app.useGlobalFilters(new AllExceptionFilter(logger, httpAdapter));

  // 全局拦截器
  app.useGlobalPipes(new ValidationPipe({
    whitelist: true, // 过滤掉所有非白名单字段
  }));

  // app.useGlobalInterceptors(new SerializeInterceptor())

  // helmet 头部安全
  app.use(helmet());

  // rateLimit 限流
  app.use(
    rateLimit({
      windowMs: 1 * 60 * 1000, // 1 minutes
      max: 100 // limit each IP to 100 requests per windowMs
    })
  )
}

